#include "strategy/simple_ma_strategy.h"
#include "strategy/optimized_ma_strategy.h"
#include "strategy/optimized_mean_reversion_strategy.h"
#include "strategy/optimized_trend_following_strategy.h"
#include "strategy/optimized_statistical_arbitrage_strategy.h"
#include <chrono>
#include <iostream>
#include <vector>
#include <random>
#include <iomanip>

// 生成随机价格数据
std::vector<double> generateRandomPrices(size_t count, double start_price = 100.0, double volatility = 0.01) {
    std::vector<double> prices;
    prices.reserve(count);
    
    std::random_device rd;
    std::mt19937 gen(rd());
    std::normal_distribution<> d(0, volatility);
    
    double price = start_price;
    for (size_t i = 0; i < count; ++i) {
        price *= (1.0 + d(gen));
        prices.push_back(price);
    }
    
    return prices;
}

// 测试移动平均策略性能
void testMaStrategyPerformance(int num_iterations = 1000000) {
    std::cout << "===== 移动平均策略性能测试 =====" << std::endl;
    
    // 生成测试数据
    auto prices = generateRandomPrices(num_iterations);
    
    // 创建策略实例
    SimpleMaStrategy original_strategy(10, 30);
    OptimizedMaStrategy optimized_strategy(10, 30);
    
    // 准备市场数据
    std::vector<MarketData> market_data;
    market_data.reserve(prices.size());
    
    for (size_t i = 0; i < prices.size(); ++i) {
        MarketData data;
        data.symbol = "TEST";
        data.timestamp = std::to_string(i);
        data.close = prices[i];
        market_data.push_back(data);
    }
    
    // 测试原始策略性能
    auto start = std::chrono::high_resolution_clock::now();
    original_strategy.initialize();
    for (const auto& data : market_data) {
        original_strategy.on_market_data(data);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto original_duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    
    // 测试优化策略性能
    start = std::chrono::high_resolution_clock::now();
    optimized_strategy.initialize();
    for (const auto& data : market_data) {
        optimized_strategy.on_market_data(data);
    }
    end = std::chrono::high_resolution_clock::now();
    auto optimized_duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    
    // 输出结果
    std::cout << "数据点数量: " << num_iterations << std::endl;
    std::cout << "原始策略耗时: " << original_duration << " ms" << std::endl;
    std::cout << "优化策略耗时: " << optimized_duration << " ms" << std::endl;
    
    double improvement = 100.0 * (original_duration - optimized_duration) / original_duration;
    std::cout << "性能提升: " << std::fixed << std::setprecision(2) << improvement << "%" << std::endl;
    std::cout << std::endl;
}

// 测试均值回归策略性能
void testMeanReversionPerformance(int num_iterations = 1000000) {
    std::cout << "===== 均值回归策略性能测试 =====" << std::endl;
    
    // 生成测试数据
    auto prices = generateRandomPrices(num_iterations);
    
    // 创建策略实例
    hft::OptimizedMeanReversionStrategy optimized_strategy("TEST", 100, 2.0, 0.5);
    
    // 准备市场数据
    std::vector<hft::MarketData> market_data;
    market_data.reserve(prices.size());
    
    for (size_t i = 0; i < prices.size(); ++i) {
        hft::MarketData data;
        data.symbol = "TEST";
        data.close = prices[i];
        market_data.push_back(data);
    }
    
    // 测试优化策略性能
    auto start = std::chrono::high_resolution_clock::now();
    optimized_strategy.start();
    for (const auto& data : market_data) {
        optimized_strategy.onMarketData(data);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    
    // 输出结果
    std::cout << "数据点数量: " << num_iterations << std::endl;
    std::cout << "优化均值回归策略耗时: " << duration << " ms" << std::endl;
    std::cout << "每秒处理数据点: " << num_iterations * 1000 / duration << std::endl;
    std::cout << std::endl;
}

// 测试趋势跟踪策略性能
void testTrendFollowingPerformance(int num_iterations = 1000000) {
    std::cout << "===== 趋势跟踪策略性能测试 =====" << std::endl;
    
    // 生成测试数据
    auto prices = generateRandomPrices(num_iterations);
    
    // 创建策略实例
    hft::OptimizedTrendFollowingStrategy optimized_strategy("TEST", 10, 30, 0.001);
    
    // 准备市场数据
    std::vector<hft::MarketData> market_data;
    market_data.reserve(prices.size());
    
    for (size_t i = 0; i < prices.size(); ++i) {
        hft::MarketData data;
        data.symbol = "TEST";
        data.close = prices[i];
        market_data.push_back(data);
    }
    
    // 测试优化策略性能
    auto start = std::chrono::high_resolution_clock::now();
    optimized_strategy.start();
    for (const auto& data : market_data) {
        optimized_strategy.onMarketData(data);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    
    // 输出结果
    std::cout << "数据点数量: " << num_iterations << std::endl;
    std::cout << "优化趋势跟踪策略耗时: " << duration << " ms" << std::endl;
    std::cout << "每秒处理数据点: " << num_iterations * 1000 / duration << std::endl;
    std::cout << std::endl;
}

// 测试统计套利策略性能
void testStatArbPerformance(int num_iterations = 1000000) {
    std::cout << "===== 统计套利策略性能测试 =====" << std::endl;
    
    // 生成测试数据
    auto prices1 = generateRandomPrices(num_iterations);
    auto prices2 = generateRandomPrices(num_iterations, 100.0, 0.01);
    
    // 创建策略实例
    hft::OptimizedStatisticalArbitrageStrategy optimized_strategy("TEST1", "TEST2", 100, 2.0, 0.5);
    
    // 准备市场数据
    std::vector<hft::MarketData> market_data;
    market_data.reserve(prices1.size() * 2);
    
    for (size_t i = 0; i < prices1.size(); ++i) {
        hft::MarketData data1;
        data1.symbol = "TEST1";
        data1.close = prices1[i];
        
        hft::MarketData data2;
        data2.symbol = "TEST2";
        data2.close = prices2[i];
        
        market_data.push_back(data1);
        market_data.push_back(data2);
    }
    
    // 测试优化策略性能
    auto start = std::chrono::high_resolution_clock::now();
    optimized_strategy.start();
    for (const auto& data : market_data) {
        optimized_strategy.onMarketData(data);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    
    // 输出结果
    std::cout << "数据点数量: " << num_iterations * 2 << " (两个交易品种)" << std::endl;
    std::cout << "优化统计套利策略耗时: " << duration << " ms" << std::endl;
    std::cout << "每秒处理数据点: " << num_iterations * 2 * 1000 / duration << std::endl;
    std::cout << std::endl;
}

int main() {
    std::cout << "开始性能测试..." << std::endl;
    std::cout << "注意：测试结果可能因硬件和系统负载而异" << std::endl << std::endl;
    
    // 运行各种策略的性能测试
    testMaStrategyPerformance(1000000);
    testMeanReversionPerformance(1000000);
    testTrendFollowingPerformance(1000000);
    testStatArbPerformance(500000);  // 统计套利处理两个品种，所以减少迭代次数
    
    std::cout << "性能测试完成!" << std::endl;
    return 0;
}